package com.oa.lms.apps.dao.notice.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.notice.IMessageInfoDao;
import com.oa.lms.apps.entity.notice.MessageInfoValue;

public class MessageInfoDaoImpl extends HibernateDaoSupport implements
		IMessageInfoDao {

	private List<MessageInfoValue> list;
	
	private Integer totalNumber;
	@Override
	public Integer add(MessageInfoValue messageInfoValue) {
		if (messageInfoValue != null) {
			this.getHibernateTemplate().save(messageInfoValue);
		}
		return messageInfoValue.getMessageId();
	}

	@Override
	public void modify(MessageInfoValue messageInfoValue) {
		if (messageInfoValue != null) {
			this.getHibernateTemplate().update(messageInfoValue);
		}

	}
	
	@Override
	public void delete(MessageInfoValue messageInfoValue) {
		if (messageInfoValue != null) {
			this.getHibernateTemplate().delete(messageInfoValue);
		}
	}

	@Override
	public MessageInfoValue queryByMessageId(Integer messageId) {
		if (messageId != null) {
			return (MessageInfoValue) this.getHibernateTemplate().get(MessageInfoValue.class, messageId);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MessageInfoValue> queryMessageInfo(
			final MessageInfoValue messageInfoValue, final Integer rows, final Integer pageNo) {
		String hqlStr = "FROM MessageInfoValue miv WHERE 1=1";
		
		if (messageInfoValue.getUserItem() != null) {
			hqlStr += " AND miv.userItem = ?";
		}
		
		if (messageInfoValue.getStatus() != null) {
			hqlStr += " AND miv.status = ?";
		}
		
		hqlStr += " ORDER BY miv.createTime DESC";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(final Session session)
					throws HibernateException, SQLException {
				final Query query = session.createQuery(hql);
				int index = -1;
				
				if (messageInfoValue.getUserItem() != null) {
					query.setEntity(++index, messageInfoValue.getUserItem());
				}
				if (messageInfoValue.getStatus() != null) {
					query.setInteger(++index, messageInfoValue.getStatus());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public Integer getTotalPageNumber(final MessageInfoValue messageInfoValue, final Integer rows){
		int totalPageNumber;
		String hqlStr = "FROM MessageInfoValue miv WHERE 1=1";
		
		if (messageInfoValue.getUserItem() != null) {
			hqlStr += " AND miv.userItem = ?";
		}
		
		if (messageInfoValue.getStatus() != null) {
			hqlStr += " AND miv.status = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(final Session session)
					throws HibernateException, SQLException {
				final Query query = session.createQuery(hql);
				int index = -1;
				
				if (messageInfoValue.getUserItem() != null) {
					query.setEntity(++index, messageInfoValue.getUserItem());
				}
				if (messageInfoValue.getStatus() != null) {
					query.setInteger(++index, messageInfoValue.getStatus());
				}
				list = query.list();
				return list;
			}
		});
		totalNumber = list.size();
		if (totalNumber <= rows) {
			totalPageNumber = 1;
		} else {
			if (totalNumber % rows == 0) {
				totalPageNumber = totalNumber / rows;
			} else {
				totalPageNumber = totalNumber / rows + 1;
			}
		}
		return totalPageNumber;
	}

	public List<MessageInfoValue> getList() {
		return list;
	}

	public void setList(List<MessageInfoValue> list) {
		this.list = list;
	}

	public Integer getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(Integer totalNumber) {
		this.totalNumber = totalNumber;
	}


}

